<?php


class Auth
{

	static $user_id = null;
	static $level = null;

	public function auth()
	{
		$CI = &get_instance();
		if(
			@$CI->uri->segments[1] == 'cron' || 
			@$CI->uri->segments[1] == 'test' || 
			@$CI->uri->segments[1] == 'update' || 
			(@$CI->uri->segments[1] == 'ajax' && @$CI->uri->segments[2] == 'forgot_password') ||
			(@$CI->uri->segments[1] == 'ajax' && @$CI->uri->segments[2] == 'get_user_des') ||
			(@$CI->uri->segments[1] == 'user' && @$CI->uri->segments[2] == 'quick_login') 
		)
		{
			return;
		}
		$error = '';
		if(!$this->_isLogin())
		{
			if(isset($_POST['login_user']) && isset($_POST['login_password']))
			{
				$users = $CI->MUser->getList(array('email' => trim($_POST['login_user'])));
				if(count($users))
				{
					$user = reset($users);
					if($user['password'] == md5(trim($_POST['login_password'])))
					{
						self::rememberUser($user['id'], $user['password']);
						redirect($CI->uri->uri_string);
					}
					else
					{
						$error = 'Пароль не подходит';
					}
				}
				else
				{
					$error = 'Не найден пользователь с таким e-mail';
				}
			}
		}
		if(!$this->_isLogin())
		{
			$this->_showLoginForm($error);
		}
		if(
			strlen(self::$level) == 0 && 
			!(
				@$CI->uri->segments[1] == 'me' || 
				(@$CI->uri->segments[1] == 'kro' && @$CI->uri->segments[2] == 'me') || 
				(@$CI->uri->segments[1] == 'stat' && @$CI->uri->segments[2] == 'calc') || 
				(@$CI->uri->segments[1] == 'stat' && @$CI->uri->segments[2] == 'mavro_kurs') || 
				(@$CI->uri->segments[1] == 'profile' && @$CI->uri->segments[2] == 'change_password') || 
				(@$CI->uri->segments[1] == 'profile' && @$CI->uri->segments[2] == 'logout')
			)
		)
		{
			redirect('me');
		}
		global $USER_SETTING;
		$USER_SETTING = $CI->MSetting->getMe();
	}

	private function _showLoginForm($error = '')
	{
		$CI = &get_instance();
		include APPPATH.'views/login.php';
//		$CI->load->view('login');
		die();
	}

	private function _isLogin()
	{
		$out = false;
		if(self::$user_id > 0)
		{
			$out = true;
		}
		else
		{
			$auth_id = intval(get_cookie('auth_id'));
			$auth_password = trim(get_cookie('auth_password'));
			if($auth_id > 0 && $auth_password)
			{
				$CI = &get_instance();
				$users = $CI->MUser->getList(array('id' => $auth_id));
				if(count($users))
				{
					$user = reset($users);
					if($user['password'] != '' && md5($user['password']) == $auth_password
//						&& $user['level'] != ''
					)
					{
						self::$user_id = $user['id'];
						self::$level = trim($user['level']);
						$out = true;
					}
				}
			}
		}
//		if(self::$user_id != 1) die('Идёт обновление. Планируемое время запуска: 17.10 по Московскому времени');
		return $out;
	}

	public static function rememberUser($user_id, $password)
	{
		set_cookie('auth_id', $user_id, time() + 60*60*24*365);
		set_cookie('auth_password', md5($password), time() + 60*60*24*365);
		self::$user_id = $user_id;
	}

	public static function logout()
	{
		delete_cookie('auth_id');
		delete_cookie('auth_password');
		self::$user_id = null;
		self::$level = null;
		$_SESSION['is_support'] = null;
	}

	public static function isSupport()
	{
		$is_support = intval(@$_SESSION['is_support']);
		$support_password = trim(@$_SESSION['support_password']);
		return $is_support > 0 && $support_password == md5(config_item('password_quick_login'));
	}



}

